//
//  XFDLogInController.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/3.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDLogInController.h"
#import <XFDCommon/HBCommon.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import "XFDLogInScrollView.h"
#import <Masonry/Masonry.h>
#import "XFDLogInAPI.h"
#import "XFDLogInNetAPI.h"
#import <XFDCommon/HBNavViewController.h>
#import <XFDCommon/UIImage+Extension.h>
#import <XFDCommon/NSDictionary+HBSafely.h>
#import <XFDCommon/XFDUserManage.h>
#import <XFDCommon/FLToast+UIView.h>
#import <XFDCommon/XFDUserModel.h>
#import <MJExtension/MJExtension.h>
@interface XFDLogInController ()

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIImageView *titleImageView;
@property (nonatomic, strong) XFDLogInScrollView *scrollview;
@property (nonatomic, strong)UIButton *registerBtn;
@property (nonatomic, strong)UIButton *changeBtn;

@end

@implementation XFDLogInController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self createSubViews];
    [self wr_setNavBarBackgroundAlpha:0];
    [self wr_setNavBarShadowImageHidden:YES];
    WEAKSELF
    self.scrollview.clickForgetPassWordBlock = ^{
        STRONGSELF
        [strongSelf.scrollview scrollToForgetView];
        [strongSelf.changeBtn setTitle:@"返回密码登录" forState:UIControlStateNormal];
        strongSelf.registerBtn.hidden = YES;
    };
    self.scrollview.logInPassWordBlock = ^(NSString *phone, NSString *passWord) {
        STRONGSELF
        NSMutableDictionary *dict  = [NSMutableDictionary dictionary];
        [dict setValueSafely:phone forKey:@"mobilephone"];
        [dict setValueSafely:passWord forKey:@"password"];
        [dict setValueSafely:[XFDUserManage getPlatform] forKey:@"platform"];
        [dict setValueSafely:[XFDLoginAPI share].role forKey:@"role"];
        [dict setValueSafely:[XFDUserManage getAppVersion] forKey:@"version"];
        [dict setValueSafely:[XFDUserManage udId] forKey:@"udid"];
        [dict setValueSafely:[XFDUserManage deviceName] forKey:@"deviceName"];
        [strongSelf loginWithPassWord:dict];
    };
    self.scrollview.logInCodeBlock = ^(NSString *phone, NSString *code) {
          STRONGSELF
        [strongSelf loginWithPhone:phone withCode:code];
    };
    
    //忘记密码
    self.scrollview.findPassWordBlock = ^(NSMutableDictionary *dict) {
        STRONGSELF
        [strongSelf findThePassWord:dict];
    };
    //获取验证码 登录
    self.scrollview.clickLogInCodeBlock = ^(NSString *phone) {
        STRONGSELF
        [strongSelf getTheCodeWithPhone:phone withISLogin:YES];
    };
    //忘记密码时获取验证码
    self.scrollview.clickForgetCodeBlock = ^(NSString *phone) {
        STRONGSELF
        [strongSelf getTheCodeWithPhone:phone withISLogin:NO];
    };
}
- (void)findThePassWord:(NSMutableDictionary *)dic{
    WEAKSELF
    [XFDLogInNetAPI  findThePassWordInfo:dic WithSuccessBlock:^(id response) {
          STRONGSELF
        [THEAPP.window makeToast:@"密码修改成功"];
        [strongSelf.scrollview scrollToPassWordView];
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
}

- (void)getTheCodeWithPhone:(NSString *)phone withISLogin:(BOOL)isLogin{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObjectSafely:phone forKey:@"mobilephone"];
    if (isLogin) {
      [dic setObjectSafely:@"LOGIN"forKey:@"type"];
    }else{
        [dic setObjectSafely:@"RESET_PASSWORD"forKey:@"type"];
    }
    WEAKSELF
    [XFDLogInNetAPI getVerificationcode:dic WithSuccessBlock:^(id response) {
        STRONGSELF
        if (isLogin) {
            [strongSelf.scrollview loginForCodeTimeAction];
        }else{
            [strongSelf.scrollview forgetForCodeTimeAction];
        }
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
    
}
- (void)loginWithPhone:(NSString *)phone withCode:(NSString *)code{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObjectSafely:phone forKey:@"mobilephone"];
    [dic setObjectSafely:code forKey:@"smsCode"];
    [dic setValueSafely:[XFDUserManage getPlatform] forKey:@"platform"];
    [dic setValueSafely:[XFDLoginAPI share].role forKey:@"role"];
    [dic setValueSafely:[XFDUserManage getAppVersion] forKey:@"version"];
    [dic setValueSafely:[XFDUserManage udId] forKey:@"udid"];
    [dic setValueSafely:[XFDUserManage deviceName] forKey:@"deviceName"];
    [XFDLogInNetAPI loginWithCode:dic WithSuccessBlock:^(id response) {
        if (response) {
            NSDictionary *dict = (NSDictionary *)response;
            [XFDUserManage saveToken: [dict objectForKeySafely:@"token"]];
            [XFDUserManage saveUserModel:[XFDUserModel mj_objectWithKeyValues:[dic objectForKeySafely:@"user"]]];
            [XFDUserManage saveAccount:phone];
            [[XFDLoginAPI share]logIn];
        }
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
}

- (void)loginWithPassWord:(NSMutableDictionary *)dict {
    [XFDLogInNetAPI loginWithPassWord:dict WithSuccessBlock:^(id response) {
        if (response) {
            NSDictionary *dic = (NSDictionary *)response;
            [XFDUserManage saveToken: [dic objectForKeySafely:@"token"]];
            [XFDUserManage saveUserModel:[XFDUserModel mj_objectWithKeyValues:[dic objectForKeySafely:@"user"]]];
            [XFDUserManage saveAccount:[dict objectForKeySafely:@"mobilephone"]];
            [XFDUserManage savePassWord:[dict objectForKeySafely:@"password"]];
             [[XFDLoginAPI share]logIn];
        }
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
}

- (void)createSubViews{
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.titleImageView];
    [self.view addSubview:self.scrollview];
    [self.view addSubview:self.registerBtn];
    [self.view addSubview:self.changeBtn];
    [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset( - 40);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
        make.top.equalTo(self.view.mas_top).with.offset(70);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.right.equalTo(self.titleImageView.mas_left);
        make.height.mas_offset(50);
        make.centerY.equalTo(self.titleImageView.mas_centerY);
    }];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(-40);
        make.width.mas_offset(100);
        make.height.mas_offset(50);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-10);
    }];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.width.mas_offset(100);
        make.height.mas_offset(50);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-10);
    }];
    [self.scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleImageView.mas_bottom).with.offset(10);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.registerBtn.mas_top);
    }];
}

- (void)changeScrollViewPage{
    NSString *str = [self.changeBtn titleForState:UIControlStateNormal];
    if ([str isEqualToString:@"验证码登录"]) {
         self.registerBtn.hidden = NO;
        [self.changeBtn setTitle:@"密码登录" forState:UIControlStateNormal];
        [self.scrollview scrollToCodeView];
        
    }else if ([str isEqualToString:@"密码登录"]){
        self.registerBtn.hidden = NO;
        [self.changeBtn setTitle:@"验证码登录" forState:UIControlStateNormal];
        [self.scrollview scrollToPassWordView];
    }else if ([str isEqualToString:@"返回密码登录"]){
        [self.changeBtn setTitle:@"验证码登录" forState:UIControlStateNormal];
        self.registerBtn.hidden = NO;
        [self.scrollview scrollToPassWordView];
        
    }
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"进入无人机管理系统";
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textColor = GlobalBlackOneColor;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UIImageView *)titleImageView{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc]init];
        _titleImageView.image = [UIImage hb_imagePathWithName:@"login_logo_ewatt" bundle:@"XFDLogin" targetClass:[self class]];
    }
    return _titleImageView;
}

- (XFDLogInScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[XFDLogInScrollView alloc]init];
        _scrollview.contentSize = CGSizeMake(SCREEN_WIDTH * 3 ,SCREEN_HEIGHT - 300);
    }
    return _scrollview;
}

- (UIButton *)changeBtn{
    if (!_changeBtn) {
        _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _changeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _changeBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_changeBtn setTitle:@"验证码登录" forState:UIControlStateNormal];
        [_changeBtn setTitleColor:[UIColor HB_ColorWithHexString:@"#3eabf9"] forState:UIControlStateNormal];
        WEAKSELF
        [_changeBtn bk_addEventHandler:^(id sender) {
            STRONGSELF
            [strongSelf changeScrollViewPage];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeBtn;
}
- (UIButton *)registerBtn{
    if (!_registerBtn) {
        _registerBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _registerBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_registerBtn setTitle:@"立即注册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor HB_ColorWithHexString:@"#3eabf9"] forState:UIControlStateNormal];
        WEAKSELF
        [_registerBtn bk_addEventHandler:^(id sender) {
            STRONGSELF
            [strongSelf.navigationController pushViewController:[XFDLoginAPI share].registerCtr animated:YES];
         
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}


@end
